﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using GP.Logic;
using GP.Logic.Model;
using System.Xml.XPath;

namespace WebForms
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //var gp = new GpContext();
            //ECG first = gp.Ecgs.First();
            //gv.DataSource = first.Values.ToList();
            //gv.DataBind();

            ////reTest.DataSource = first.Values.ToList();
            ////reTest.DataBind();

            //IList<Container> list = new List<Container>();

            //Container con = new Container();
            
            //XmlDataSource xmlSource = new XmlDataSource();
            //xmlSource.DataFile = "~/test.xml";
            //xmlSource.XPath = "CitizenVitalSign";
            //con.Source = xmlSource;
            //list.Add(con);

            //xmlSource = new XmlDataSource();
            //xmlSource.DataFile = "~/test.xml";
            //xmlSource.XPath = "CitizenVitalSign";
            //con.Source = xmlSource;
            //list.Add(con);
            
            //re.DataSource = list;
            //re.DataBind();

            if(!IsPostBack)
            {
                XDocument doc = XDocument.Load(Server.MapPath("~/test.xml"));
                //var oximeters = doc.Root
                //        .XPathSelectElements(@"VitalSigns/Signs/Oximeter")
                //        .Select(a => new { 
                //            SaturationLevel = a.Element("SaturationLevel").Value, 
                //            HeartRate = a.Element("HeartRate").Value }).ToList();

                var ecgs = doc.Root
                    .XPathSelectElements(@"VitalSigns/Signs/ECG/ECGValues")
                    .Select(a => new
                                     {
                                         Values = string.Join("<br />", a.Elements("value").Select(x => x.Value).ToArray())
                                     }).ToList();

                var weight = doc.Root
                    .XPathSelectElements(@"VitalSigns/Signs/Weight")
                    .Select(a => new {weightVal = a.Element("WeigthInGrams").Value});

               
                gv.DataSource = ecgs;
                gv.DataBind();


            }
        }

        protected void gv_DataBinding(object sender, EventArgs e)
        {
            
            //foreach (BoundField column in gv.Columns)
            //{
            //    column.HtmlEncode = true;
            //}
        }

        protected void gv_DataBound(object sender, EventArgs e)
        {
            //foreach (BoundField column in gv.Columns)
            //{
            //    column.HtmlEncode = false;
            //}
        }

        protected void gv_SelectedIndexChanged(object sender, EventArgs e)
        {
                    
        }

        //save operation
        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gv.Rows)
            {
                var checkBox = row.Cells[1].FindControl("CheckBox1") as CheckBox;
                if (checkBox == null)
                    return;

                if (checkBox.Checked)
                {
                    string[] strings = row.Cells[0].Text.Split(new []{"<br />"}, StringSplitOptions.RemoveEmptyEntries);

                    var context = new GpContext();

                    ECG ecg = new ECG();
                    // ecg.CitizenId = 1;
                    // ecg.EventId = 3;
                    ecg.Type = "E";

                    char i = 'A';
                    foreach (var s in strings)
                    {
                        ECGValue value = new ECGValue();
                        value.Value = s;
                        value.ECGID = new string(i, 1);
                        i++;
                        ecg.Values.Add(value);
                    }

                    context.Ecgs.Add(ecg);
                    context.SaveChanges();
                }
            }
        }   
    }

    public class Container
    {
        public XmlDataSource Source { get; set; }
    }
    
}